﻿using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using OpenLS.Spreadsheet.Formats.Biff.Records;

namespace OpenLS.Spreadsheet.Formats.Biff
{
    internal class Sequencer
    {
        protected static IEnumerable<BiffRecord> Concat(params object[] objects)
        {
            return Concat_(objects);
        }

        private static IEnumerable<BiffRecord> Concat_(IEnumerable<object> objects)
        {
            foreach (object o in objects)
            {
                if (o is BiffRecord)
                {
                    Contract.Assert(!(o is IgnoredBiffRecord));
                    yield return (BiffRecord)o;
                }
                else if (o is IEnumerable<BiffRecord>)
                {
                    IEnumerable<BiffRecord> records = (IEnumerable<BiffRecord>)o;
                    foreach (var r in records) yield return r;
                }
                else if (o is IEnumerable<object>)
                {
                    IEnumerable<object> oo = (IEnumerable<object>)o;
                    foreach (var biffRecord in Concat_(oo))
                    {
                        yield return biffRecord;
                    }

                }
                else
                {
                    Contract.Assert(false, o.GetType().FullName);
                    throw new NotSupportedException();
                }
            }
        }
    }
}